//settingPage

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_uxin/Tools/config/commonTool.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

class settingPage extends StatefulWidget {
  settingPage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _settingPageState createState() => _settingPageState();
}

class _settingPageState extends State<settingPage> {
  bool _loading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body:
          ModalProgressHUD(child: _buildWidget(context), inAsyncCall: _loading),
    );
    // This trailing comma makes auto-formatting nicer for build methods.
  }

  Widget _buildWidget(BuildContext context) {

    //退出登录 并清理缓存
    logoutAndclear() async {
//      bool isSucess = await commonTool.clearPreferencesValue();

      setState(() {
        _loading = false;
      });
      Navigator.of(context).pushReplacementNamed(
        "LoginRoot",
      );
//      if (isSucess == true) {
//        Navigator.of(context).pushReplacementNamed(
//          "LoginRoot",
//        );
//      } else {
//        Navigator.of(context).pushReplacementNamed(
//          "LoginRoot",
//        );
//      }
    }


    return Container(
      color: Color(0xffeaeaea),
//        padding:EdgeInsets.only(top: 10),

      child: ListView(
        children: <Widget>[
          MineItemWidget("lib/My/images/wset_remind.png", "提醒设置", onTap: () {}),
          MineItemWidget("lib/My/images/wset_eyecare.png", "护眼模式",
              onTap: () {}),
          _listViewLine,
          MineItemWidget("lib/My/images/wset_change.png", "登录角色切换",
              onTap: () {}),
          MineItemWidget("lib/My/images/wset_event.png", "清除缓存", onTap: () {}),
          MineItemWidget("lib/My/images/wset_switch.png", "修改密码", onTap: () {}),
          _listViewLine,
          MineItemWidget("lib/My/images/wset_about.png", "关于优教信使",
              onTap: () {}),
          Container(
            padding: EdgeInsets.only(left: 25, right: 25),
            child: FlatButton(
              child: Text(
                '退  出  登  录',
                style: TextStyle(
                  fontSize: 16.0,
                  color: Colors.white,
                ),
              ),
              color: Colors.red,
              textColor: Colors.white,
              onPressed: () {

                setState(() {
                  _loading = true;
                });

                logoutAndclear();
//                       Navigator.push(context, MaterialPageRoute(builder: (context) => MyHomePage()));
//                        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => MyHomePage()));
              },
            ),
          ),
        ],
      ),
    );
  }
}

class MineTopWidget extends StatelessWidget {
  String imageName;
  String title;
  @required
  VoidCallback onTap;

  MineTopWidget(this.imageName, this.title, {this.onTap});
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      children: <Widget>[
        Container(
          color: Colors.white,
          height: 80,
          child: _mineItem(imageName, title),
        ),
        Container(
          color: Color(0xffeaeaea),
          constraints: BoxConstraints.expand(height: 1.0),
        ),
      ],
    ));
  }

  Widget _mineItem(String imageName, String title) {
    return InkWell(
      onTap: () {
        this.onTap();
      },
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 2,
            child: Container(
                padding: EdgeInsets.only(left: 16),
                child: CachedNetworkImage(
                  imageUrl:
                      "https://static001.geekbang.org/resource/image/7d/eb/7d5a36a8727ed68f241719b7768262eb.jpg",
                  placeholder: (context, url) => CircularProgressIndicator(),
                  errorWidget: (context, url, error) => Icon(Icons.error),
                )),
          ),
          Expanded(
            flex: 7,
            child: Container(
              padding: EdgeInsets.only(left: 10),
              child: Text(
                title,
                style: TextStyle(fontSize: 16),
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(
              child: Icon(
                Icons.keyboard_arrow_right,
                size: 20,
                color: Colors.grey,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class MineItemWidget extends StatelessWidget {
  String imageName;
  String title;
  @required
  VoidCallback onTap;

  MineItemWidget(this.imageName, this.title, {this.onTap});
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      children: <Widget>[
        Container(
          color: Colors.white,
          height: 53,
          child: _mineItem(imageName, title),
        ),
        Container(
          color: Color(0xffeaeaea),
          constraints: BoxConstraints.expand(height: 1.0),
        ),
      ],
    ));
  }

  Widget _mineItem(String imageName, String title) {
    return InkWell(
      onTap: () {
        this.onTap();
      },
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Container(
              padding: EdgeInsets.only(left: 16),
              child: Image.asset(
                imageName,
                width: 30,
                height: 30,
              ),
            ),
          ),
          Expanded(
            flex: 6,
            child: Container(
              padding: EdgeInsets.only(left: 10),
              child: Text(
                title,
                style: TextStyle(fontSize: 16),
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(
              child: Icon(
                Icons.keyboard_arrow_right,
                size: 20,
                color: Colors.grey,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

// 分割线
Widget get _listViewLine {
  return Container(
    color: Color(0xffeaeaea),
    height: 6,
  );
}
